<?php
/*
 *   商家优惠券
 *  */
namespace Home\Controller;
use Think\Controller;
class CouponsController extends Controller {
    //商家优惠券发布列表
    public function index(){
        $uid = $_SESSION['uid']?$_SESSION['uid']:'1';
        $coupon = M('discount_coupon')->where(array('user_id'=>$uid))->select();
        print_r($coupon);die;
        $this->display();
    }
    //商家发布优惠券
    public function add(){
        $data['user_id'] = 1;
        $data['title'] = I('post.title')?I('post.title'):$this->error('标题不能为空！');
        $data['price'] = I('post.price')?I('post.price'):$this->error('优惠金额不能为空！');
        $data['start_time'] = I('post.start_time')?I('post.start_time'):$this->error('优惠券有效时间不能为空！');
        $data['end_time'] = I('post.end_time')?I('post.end_time'):$this->error('优惠券有效时间不能为空！');
        $data['status'] = I('post.status')?I('post.status'):$this->error('请填写优惠券类型！');
        $data['add_time'] = I('post.add_time');
        $data['type'] = I('post.type')?I('post.type'):$this->error('请填写发布优惠券来源！');
        $data['condition_price'] = I('post.condition_price')?I('post.condition_price'):$this->error('请填写优惠券抵扣金额！');
        $res = M('discount_coupon')->add($data);
        if($res){
            //发布成功，扣除发布优惠券钱（商家支出）
            $this->success('发布商家优惠券成功！');
        }else{
            $this->error('发布商家优惠券失败！');
        }
    }
    //删除已经发布的优惠券（不一定需要删除）
    public function del(){
        $id = I('post.id')?I('post.id'):$this->error('请选择要删除的优惠券！');
        $oneCoupon = M('discount_coupon')->field('end_time')->where(array('id'=>$id))->find();
        $date = date('Y-m-d H:i:s');
        if($oneCoupon['end_time']<$date){
            $this->error('发布的优惠券还没有过期，禁止删除');
        }
        $res = M('discount_coupon')->where(array('id'=>$id))->delete();
        if($res) $this->success('删除成功'); else $this->error('删除失败！');
    }
} 